home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 336 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  68 lines

  1. Path: ludwig.esd.sgi.com!dscott
  2. From: dscott@ludwig.esd.sgi.com (Douglas Scott)
  3. Newsgroups: comp.lang.c++
  4. Subject: Forcing instantiation of static methods in template classes
  5. Date: 3 Jan 1996 22:23:06 GMT
  6. Organization: Silicon Graphics, Inc., Mountain View, CA
  7. Message-ID: <4cevka$ct3@fido.asd.sgi.com>
  8. NNTP-Posting-Host: ludwig.esd.sgi.com
  9.  
  10. I have a series of template classes which all contain a static member
  11. function, rather like this:
  12.  
  13. class FooBase {
  14. };
  15.  
  16. template <class T>
  17. class Foo : public FooBase {
  18. public:
  19.     Foo() {}
  20.     static FooBase* create();
  21. };
  22.  
  23. template <class T>
  24. FooBase* Foo<T>::create() { return new Foo<T>; }
  25.  
  26.  
  27. I assumed that if I created a static struct which included a pointer to each
  28. static member function that I wished to have instantiated:
  29.  
  30. typedef FooBase* (*FooFun)();
  31.  
  32. FooFun funs[] = {
  33.     &Foo<char>::create, &Foo<short>::create, &Foo<int>::create
  34. };
  35.  
  36.  
  37. ... that the compiler would be forced to instantiate the template classes
  38. Foo<char>, Foo<short>, and Foo<int>, respectively.  Upon compiling the file
  39. containing the template function declarations and definitions plus these
  40. static arrays of pointers-to-static-member-functions, and then linking that
  41. object against another file to generate an executable, I get linker errors
  42. indicating that, in effect, none of these static functions are being created,
  43. because their symbols are not found.  Here is the beginning of the actual
  44. error messages from the real source:
  45.  
  46. ld:
  47. Unresolved:
  48. ChannelChange<float>::create(const char*,int,int,const PCMMap&,double*,unsigned int,void*,int*)
  49. ChannelChange<double>::create(const char*,int,int,const PCMMap&,double*,unsigned int,void*,int*)
  50. ChannelChange<int>::create(const char*,int,int,const PCMMap&,double*,unsigned int,void*,int*)
  51. Swap<int>::create(const char*,void*,int*)
  52. Swap<float>::create(const char*,void*,int*)
  53. ...
  54. etc.
  55.  
  56.  
  57. Am I making an invalid assumption?  Or is this a compiler-dependent behavior?
  58. Given that I have a finite and determined number of template class
  59. instantiations, is there another way to force the instantiation of them?
  60.  
  61. Thanks.
  62.  
  63. -- 
  64. ---
  65. Douglas Scott                       | Silicon Graphics, Inc.
  66. dscott@esd.sgi.com                  | Digital Media Systems 
  67. (415) 933-6404  Fax: (415) 390-6159 | http://reality.sgi.com/employees/dscott
  68.